package leetcode.math;

import java.util.HashMap;

/**
 * https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/
 *
 * @author mazouri
 * @create 2021-10-02 23:11
 */
public class _405_toHex {
    public String toHex(int num) {
        if (num == 0) return "0";

        long x = num;
        StringBuilder res = new StringBuilder();
        String dict = "0123456789abcdef";

        //补码 负数+2*32
        if (num < 0) x = x + 4294967296L;
        int remainder;
        while (x != 0) {
            remainder = (int) (x % 16);
            res.insert(0, dict.charAt(remainder));
            x = x / 16;
        }
        return res.toString();
    }

    public static void main(String[] args) {
        System.out.println((long) Math.pow(2, 32));
        System.out.println(new _405_toHex().toHex(-1));
    }
}
